package com.analyse.stock.model;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.Objects;

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("t_fund_flow_history")
public class FundFlowHistoryModel extends BaseModel<FundFlowHistoryModel> {

    private String stockCode;

    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private Date tradeDate;

    private BigDecimal price;

    private BigDecimal upDownPercent;

    private BigDecimal mainAmt;
    private BigDecimal mainAmtPercent;
    /**
     * 当日拥有股本总数 ，计算方式，主力净流入/今日股票均价
     * 今日股票均价 = 今日总成交额/(今日总成交量(手)*100)
     **/
    private BigDecimal mainCapitalCount;

    private BigDecimal moreBigOrderAmt;
    private BigDecimal moreBigOrderPercent;
    private BigDecimal bigOrderAmt;
    private BigDecimal bigOrderPercent;
    private BigDecimal midOrderAmt;
    private BigDecimal midOrderPercent;
    private BigDecimal smallOrderAmt;
    private BigDecimal smallOrderPercent;

    //散户净买金额
    private BigDecimal flowAmt;
    private BigDecimal flowAmtPercent;

    private Integer partitionKey;

    public static FundFlowHistoryModel transFundFlowHistoryModel(String[] str,String stockCode){
        if(str == null || str.length<13){
            return null;
        }
        FundFlowHistoryModel model = new FundFlowHistoryModel();
        model.setTradeDate(DateUtil.parseDate(str[0]));
        model.setPartitionKey(DateUtil.year(model.getTradeDate()));
        model.setMainAmt(new BigDecimal(str[1]));
        model.setSmallOrderAmt(new BigDecimal(str[2]));
        model.setMidOrderAmt(new BigDecimal(str[3]));
        model.setBigOrderAmt(new BigDecimal(str[4]));
        model.setMoreBigOrderAmt(new BigDecimal(str[5]));
        model.setMainAmtPercent(new BigDecimal(str[6]));
        model.setSmallOrderPercent(new BigDecimal(str[7]));
        model.setMidOrderPercent(new BigDecimal(str[8]));
        model.setBigOrderPercent(new BigDecimal(str[9]));
        model.setMoreBigOrderPercent(new BigDecimal(str[10]));
        model.setPrice(new BigDecimal(str[11]));
        model.setUpDownPercent(new BigDecimal(str[12]));
        model.setStockCode(stockCode);
        //散户单  中单净额+小单净额
        model.setFlowAmt(model.getMidOrderAmt().add(model.getSmallOrderAmt()));

        //先算出当日总的成交额   主力净流入的占比（(主力净流入/当日成交额) = 1.2/100）
        if(model.getMainAmtPercent().compareTo(BigDecimal.ZERO)>0){
            BigDecimal amount = model.getMainAmt().divide(model.getMainAmtPercent(),8, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
            BigDecimal flowAmountPercent = model.getFlowAmt().divide(amount,4, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
            model.setFlowAmtPercent(flowAmountPercent);
        }
        model.setCreateTime(new Date());
        model.setUpdateTime(new Date());
        return model;
    }

    public static FundFlowHistoryModel me(String stockCode,Date tradeDate){
        FundFlowHistoryModel model = new FundFlowHistoryModel();
        model.setStockCode(stockCode);
        model.setTradeDate(tradeDate);
        if(Objects.nonNull(tradeDate)){
            model.setPartitionKey(DateUtil.year(tradeDate));
        }
        return model;
    }
}
